package com.learn.controller;

import com.learn.exception.AppException;
import com.learn.exception.BalanceNotEnoughException;
import com.learn.service.AccountService;
import com.learn.service.impl.AccountServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/02/20
 * @description
 */
@WebServlet("/transfer")
public class AccountController extends HttpServlet {
    private AccountService accountService = new AccountServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fromId = request.getParameter("fromId");
        String toId = request.getParameter("toId");
        double money = Double.parseDouble(request.getParameter("money"));
        try {
            accountService.transfer(fromId, toId, money);
            response.getWriter().write("交易成功");
        } catch (BalanceNotEnoughException | AppException e) {
            response.getWriter().write(e.getMessage());
        } catch (Exception e) {
            response.getWriter().write(e.getMessage());
        }
    }
}
